package com.erb.controller.zwj.admin;

import com.erb.constants.AdminProductConstants;
import com.erb.entity.Pager;
import com.erb.entity.Product;
import com.erb.entity.ProductExtends;
import com.erb.service.zwj.IProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/product")
public class AdminProductController {
	@Resource
	private IProductService productService;

	@GetMapping("/list")
	public String list(Model model, Pager pager, ProductExtends productExtends) {
		pager.page(productService.findCount(productExtends));

		model.addAttribute("pager", pager);
		model.addAttribute("productList", productService.findAllForSimpleList(pager, productExtends));
		// 最小年化利率
		model.addAttribute("productRateMin", AdminProductConstants.PRODUCT_RATE_MIN);
		// 最大年化利率
		model.addAttribute("productRateMax", AdminProductConstants.PRODUCT_RATE_MAX);
		// 最小起购金额
		model.addAttribute("productPurchaseAmount", AdminProductConstants.PRODUCT_PURCHASE_AMOUNT);
		// 项目最小总金额
		model.addAttribute("productTotalMin", AdminProductConstants.PRODUCT_TOTAL_MIN);
		// 项目最大总金额
		model.addAttribute("productTotalMax", AdminProductConstants.PRODUCT_TOTAL_MAX);
		// 项目锁定期，数组
		model.addAttribute("productLockDateMonth", AdminProductConstants.PRODUCT_LOCK_DATE_MONTH);
		// 项目类型，Map
		model.addAttribute("productTypeMap", AdminProductConstants.PRODUCT_TYPE_MAP);
		// 项目还款类型，Map
		model.addAttribute("productRepayTypeMap", AdminProductConstants.PRODUCT_REPAY_TYPE_MAP);
		// 项目状态，Map
		model.addAttribute("productStatusMap", AdminProductConstants.PRODUCT_STATUS_MAP);

		// 当前时间，用于限制项目添加的开始时间
		model.addAttribute("minDate", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(new Date()));
		return "admin/product/list";
	}

	/**
	 * AJAX条件查询列表
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/listBySearch")
	public Object listBySearch(Pager pager, ProductExtends productExtends) {

		return productService.findAllForSimpleList(pager, productExtends);
	}

	/**
	 * 通过ID获取项目对象信息
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/{id}")
	public Map<String, Object> get(@PathVariable("id") Integer id) {
		Map<String, Object> map = new HashMap<>(3);

		Product productById = productService.findProductById(id);
		if (null == productById) {
			map.put("code", 1);
			map.put("msg", "该项目不存在！");
		} else {
			map.put("code", 0);
			map.put("msg", "查找成功！");
			map.put("object", productById);
		}

		return map;
	}

	@ResponseBody
	@PostMapping("/save")
	public Map<String, Object> save(Product product, String startDateTime) {
		Assert.notNull(product, "参数product不存在！");
		Assert.notNull(startDateTime, "参数startDateTime不存在！");

		Map<String, Object> map = new HashMap<>(2);

		// 格式化开始时间
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
		try {
			product.setStartDate(new Timestamp(dateFormat.parse(startDateTime).getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 项目上新
		product.setStatus(AdminProductConstants.PRODUCT_STATUS_BIDS);// 项目招标中
		product.setHaveMoney(AdminProductConstants.PRODUCT_DEFAULT_HAVE_MONEY);// 默认已投金额0.0
		product.setRepayType(AdminProductConstants.PRODUCT_REPAY_TYPE_END);// 默认还款方式：到期还本付息

		if (productService.saveProduct(product)) {
			map.put("code", 0);
			map.put("msg", "保存成功！");
		} else {
			map.put("code", 1);
			map.put("msg", "保存失败，请重试。");
		}
		return map;
	}

	/*@ResponseBody
	@PostMapping("/edit")
	public Map<String, Object> edit(Product product, String startDateTime) {
		product.setStartDate(new Timestamp(Long.parseLong(startDateTime)));// 项目开始时间
		System.out.println(product);
		return null;
	}*/

	@ResponseBody
	@PostMapping("/remove/{id}")
	public Map<String, Object> remove(@PathVariable("id") Integer id) {
		Assert.notNull(id, "参数ID不能为NUll！");

		Map<String, Object> map = new HashMap<>(2);
		boolean b = productService.deleteProduct(id);
		if (b) {
			map.put("code", 0);
			map.put("msg", "删除成功！");
		} else {
			map.put("code", 1);
			map.put("msg", "删除失败！");
		}
		return map;
	}
}
